﻿using UnityEngine;
using Yoozoo.Mars.Got.SpriteAnim;

namespace Yoozoo.Gameplay.RTS
{
    public class RTSSpriteHelper : Interface_AnimSpriteSupportTemplate
    {
        public static readonly Vector3 LEFT_DOWN = (Vector3.left + Vector3.down).normalized;
        public static readonly Vector3 RIGHT_DOWN = (Vector3.right + Vector3.down).normalized;
        public static readonly Vector3 LEFT_UP = (Vector3.left + Vector3.up).normalized;
        public static readonly Vector3 RIGHT_UP = (Vector3.right + Vector3.up).normalized;

        private static float DEGREE_THRESHOLD = 22.5f;//DataManager.Instance.worldDataMgr.CfgData.KVCfgMgr.GetFloatValue("INTERNAL_ANGLE", 45f);//Jiaqi fix the code
        private static float DEGREE_THRESHOLD_16Direct = 11.25f;
        public static float COS_THRESHOLD = Mathf.Cos(DEGREE_THRESHOLD * Mathf.Deg2Rad);
        private Vector3 tempVector3 = Vector3.zero;

        public DIRCTION_INDEX_ENUM GetDirectionIndex(Vector3 moveVector)
        {
            tempVector3.x = moveVector.x;
            tempVector3.y = moveVector.z;
            Vector3 dir = tempVector3;
            DIRCTION_INDEX_ENUM dirIdx = 0;

            var originForward = Quaternion.Euler(0, 45, 0) * new Vector3(0, 0, 1);
            var angle = 0f;
            if (moveVector.x > 0)
            {
                angle = (360 - Vector3.Angle(moveVector,originForward));
            }
            else
            {
                angle = Vector3.Angle(moveVector,originForward);
            }

            if (angle < DEGREE_THRESHOLD || angle > 360 - DEGREE_THRESHOLD)
            {
                dirIdx = DIRCTION_INDEX_ENUM.UP;
            }
            else if (angle <= 45 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 45)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_UP;
            }
            else if (angle <= 90 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 90)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT;
            }
            else if (angle <= 135 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 135)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_DOWN;
            }
            else if (angle <= 180 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 180)
            {
                dirIdx = DIRCTION_INDEX_ENUM.DOWN;
            }
            else if (angle <= 225 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 225)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_DOWN;
            }
            else if (angle <= 270 + DEGREE_THRESHOLD && angle >= DEGREE_THRESHOLD - 270)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT;
            }
            else if (angle <= 315 + DEGREE_THRESHOLD && angle > DEGREE_THRESHOLD - 315)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            }
            return dirIdx;
        }
        
        public DIRCTION_INDEX_ENUM GetDirectionIndex16Direct(Vector3 moveVector)
        {
            tempVector3.x = moveVector.x;
            tempVector3.y = moveVector.z;
            Vector3 dir = tempVector3;
            DIRCTION_INDEX_ENUM dirIdx = 0;

            var originForward = new Vector3(0,0, 1);
            var angle = 0f;
            if (moveVector.x > 0)
            {
                angle = (360 - Vector3.Angle(moveVector,originForward));
            }
            else
            {
                angle = Vector3.Angle(moveVector,originForward);
            }
            if (angle < DEGREE_THRESHOLD_16Direct || angle > 360 - DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.UP;
            }
            else if (angle <= 22.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFTUP_MIDDLE_UP;
            }
            else if (angle <= 45 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_UP;
            }
            else if (angle <= 67.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_MIDDLE_LEFTUP;
            }
            else if (angle <= 90 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT;
            }
            else if (angle <= 112.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFTDOWN_MIDDLE_LEFT;
            }
            else if (angle <= 135 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.LEFT_DOWN;
            }
            else if (angle <= 157.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.DOWN_MIDDLE_LEFTDOWN;
            }
            else if (angle <= 180 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.DOWN;
            }
            else if (angle <= 202.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHTDOWN_MIDDLE_DOWN;
            }
            else if (angle <= 225 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_DOWN;
            }
            else if (angle <= 247.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_MIDDLE_RIGHTDOWN;
            }
            else if (angle <= 270 + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT;
            }
            else if (angle <= 292.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHTUP_MIDDLE_RIGHT;
            }
            else if (angle <= 315 + DEGREE_THRESHOLD_16Direct && angle > DEGREE_THRESHOLD_16Direct - 315)
            {
                dirIdx = DIRCTION_INDEX_ENUM.RIGHT_UP;
            }
            else if (angle <= 337.5f + DEGREE_THRESHOLD_16Direct)
            {
                dirIdx = DIRCTION_INDEX_ENUM.UP_MIDDLE_RIGHTUP;
            }
            
            return dirIdx;
        }
        
        private static float MyAbs(float a) {
            return a < 0 ? -a : a;
        }
        public Material GetMaterial(string altasName)
        {
            Material material = new Material(RTSResourceManager.MarchMaterial);
            return material;
        }

        public Vector3 GetBillBoardRotation()
        {
            return new Vector3(315, 315, 0);
        }
    }
}
